Imports cmplib01

Public Class ctlBattleAssets
    Inherits System.Windows.Forms.UserControl

    Public Event ChitChangedOwnership()
    Public Event UnitKilledOrUnkilled()

    Private m_DefenderView As Boolean = False ' we default to attacker
    Private m_SelectedBattle As Battle
    Private ptrGameData As CampaignerGameData
    Private owningFactionID As Integer

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'UserControl overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents pnlAttackerStuff As System.Windows.Forms.Panel
    Friend WithEvents btnMovement As System.Windows.Forms.Button
    Friend WithEvents btnPointValue As System.Windows.Forms.Button
    Friend WithEvents btnOwner As System.Windows.Forms.Button
    Friend WithEvents btnKillChit As System.Windows.Forms.Button
    Friend WithEvents btnEditElements As System.Windows.Forms.Button
    Friend WithEvents lblManagement As System.Windows.Forms.Label
    Friend WithEvents imlIcons As System.Windows.Forms.ImageList
    Friend WithEvents tvwUnits As System.Windows.Forms.TreeView
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(ctlBattleAssets))
        Me.pnlAttackerStuff = New System.Windows.Forms.Panel
        Me.tvwUnits = New System.Windows.Forms.TreeView
        Me.imlIcons = New System.Windows.Forms.ImageList(Me.components)
        Me.btnMovement = New System.Windows.Forms.Button
        Me.btnPointValue = New System.Windows.Forms.Button
        Me.btnOwner = New System.Windows.Forms.Button
        Me.btnKillChit = New System.Windows.Forms.Button
        Me.btnEditElements = New System.Windows.Forms.Button
        Me.lblManagement = New System.Windows.Forms.Label
        Me.pnlAttackerStuff.SuspendLayout()
        Me.SuspendLayout()
        '
        'pnlAttackerStuff
        '
        Me.pnlAttackerStuff.BackColor = System.Drawing.Color.LightSkyBlue
        Me.pnlAttackerStuff.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.pnlAttackerStuff.Controls.Add(Me.tvwUnits)
        Me.pnlAttackerStuff.Controls.Add(Me.btnMovement)
        Me.pnlAttackerStuff.Controls.Add(Me.btnPointValue)
        Me.pnlAttackerStuff.Controls.Add(Me.btnOwner)
        Me.pnlAttackerStuff.Controls.Add(Me.btnKillChit)
        Me.pnlAttackerStuff.Controls.Add(Me.btnEditElements)
        Me.pnlAttackerStuff.Controls.Add(Me.lblManagement)
        Me.pnlAttackerStuff.Dock = System.Windows.Forms.DockStyle.Fill
        Me.pnlAttackerStuff.Location = New System.Drawing.Point(0, 0)
        Me.pnlAttackerStuff.Name = "pnlAttackerStuff"
        Me.pnlAttackerStuff.Size = New System.Drawing.Size(308, 264)
        Me.pnlAttackerStuff.TabIndex = 4
        '
        'tvwUnits
        '
        Me.tvwUnits.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
        Me.tvwUnits.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.tvwUnits.ImageIndex = 0
        Me.tvwUnits.ImageList = Me.imlIcons
        Me.tvwUnits.Location = New System.Drawing.Point(-1, 23)
        Me.tvwUnits.Name = "tvwUnits"
        Me.tvwUnits.SelectedImageIndex = 0
        Me.tvwUnits.Size = New System.Drawing.Size(196, 240)
        Me.tvwUnits.TabIndex = 0
        '
        'imlIcons
        '
        Me.imlIcons.ImageStream = CType(resources.GetObject("imlIcons.ImageStream"), System.Windows.Forms.ImageListStreamer)
        Me.imlIcons.TransparentColor = System.Drawing.Color.Transparent
        Me.imlIcons.Images.SetKeyName(0, "")
        Me.imlIcons.Images.SetKeyName(1, "")
        Me.imlIcons.Images.SetKeyName(2, "")
        Me.imlIcons.Images.SetKeyName(3, "")
        Me.imlIcons.Images.SetKeyName(4, "")
        Me.imlIcons.Images.SetKeyName(5, "")
        Me.imlIcons.Images.SetKeyName(6, "")
        '
        'btnMovement
        '
        Me.btnMovement.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.btnMovement.BackColor = System.Drawing.Color.DarkSeaGreen
        Me.btnMovement.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnMovement.Image = CType(resources.GetObject("btnMovement.Image"), System.Drawing.Image)
        Me.btnMovement.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.btnMovement.Location = New System.Drawing.Point(194, 219)
        Me.btnMovement.Name = "btnMovement"
        Me.btnMovement.Size = New System.Drawing.Size(118, 23)
        Me.btnMovement.TabIndex = 7
        Me.btnMovement.Text = "Movement"
        Me.btnMovement.UseVisualStyleBackColor = False
        Me.btnMovement.Visible = False
        '
        'btnPointValue
        '
        Me.btnPointValue.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.btnPointValue.BackColor = System.Drawing.Color.DarkSeaGreen
        Me.btnPointValue.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnPointValue.Image = CType(resources.GetObject("btnPointValue.Image"), System.Drawing.Image)
        Me.btnPointValue.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.btnPointValue.Location = New System.Drawing.Point(194, 195)
        Me.btnPointValue.Name = "btnPointValue"
        Me.btnPointValue.Size = New System.Drawing.Size(118, 23)
        Me.btnPointValue.TabIndex = 6
        Me.btnPointValue.Text = "Point Value"
        Me.btnPointValue.UseVisualStyleBackColor = False
        Me.btnPointValue.Visible = False
        '
        'btnOwner
        '
        Me.btnOwner.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.btnOwner.BackColor = System.Drawing.Color.DarkSeaGreen
        Me.btnOwner.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnOwner.Image = CType(resources.GetObject("btnOwner.Image"), System.Drawing.Image)
        Me.btnOwner.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.btnOwner.Location = New System.Drawing.Point(194, 59)
        Me.btnOwner.Name = "btnOwner"
        Me.btnOwner.Size = New System.Drawing.Size(118, 23)
        Me.btnOwner.TabIndex = 2
        Me.btnOwner.Text = "Owner"
        Me.btnOwner.UseVisualStyleBackColor = False
        '
        'btnKillChit
        '
        Me.btnKillChit.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.btnKillChit.BackColor = System.Drawing.Color.DarkSeaGreen
        Me.btnKillChit.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnKillChit.Image = CType(resources.GetObject("btnKillChit.Image"), System.Drawing.Image)
        Me.btnKillChit.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.btnKillChit.Location = New System.Drawing.Point(194, 32)
        Me.btnKillChit.Name = "btnKillChit"
        Me.btnKillChit.Size = New System.Drawing.Size(118, 23)
        Me.btnKillChit.TabIndex = 1
        Me.btnKillChit.Text = "Kill"
        Me.btnKillChit.UseVisualStyleBackColor = False
        '
        'btnEditElements
        '
        Me.btnEditElements.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.btnEditElements.BackColor = System.Drawing.Color.DarkSeaGreen
        Me.btnEditElements.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnEditElements.Image = CType(resources.GetObject("btnEditElements.Image"), System.Drawing.Image)
        Me.btnEditElements.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.btnEditElements.Location = New System.Drawing.Point(194, 147)
        Me.btnEditElements.Name = "btnEditElements"
        Me.btnEditElements.Size = New System.Drawing.Size(118, 23)
        Me.btnEditElements.TabIndex = 3
        Me.btnEditElements.Text = "Elements"
        Me.btnEditElements.UseVisualStyleBackColor = False
        Me.btnEditElements.Visible = False
        '
        'lblManagement
        '
        Me.lblManagement.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.lblManagement.BackColor = System.Drawing.Color.WhiteSmoke
        Me.lblManagement.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.lblManagement.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblManagement.Location = New System.Drawing.Point(-1, -1)
        Me.lblManagement.Name = "lblManagement"
        Me.lblManagement.Size = New System.Drawing.Size(309, 25)
        Me.lblManagement.TabIndex = 1
        Me.lblManagement.Text = "Attacker Chits"
        Me.lblManagement.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'ctlBattleAssets
        '
        Me.Controls.Add(Me.pnlAttackerStuff)
        Me.Name = "ctlBattleAssets"
        Me.Size = New System.Drawing.Size(308, 264)
        Me.pnlAttackerStuff.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    Public Property GameData() As CampaignerGameData
        Get
            Return ptrGameData
        End Get
        Set(ByVal Value As CampaignerGameData)
            ptrGameData = Value
        End Set
    End Property

    Public Property DefenderView() As Boolean
        Get
            Return m_DefenderView
        End Get
        Set(ByVal Value As Boolean)
            m_DefenderView = Value

            If m_DefenderView = True Then
                lblManagement.Text = "Defender Assets"
            Else
                lblManagement.Text = "Attacker Assets"
            End If

        End Set
    End Property

    Public WriteOnly Property SelectedBattle() As Battle
        Set(ByVal Value As Battle)
            m_SelectedBattle = Value

            If m_SelectedBattle Is Nothing Then Exit Property

            If m_DefenderView Then
                owningFactionID = m_SelectedBattle.DefendingFactionID
                ListForFactionID(m_SelectedBattle.DefendingFactionID)
            Else
                owningFactionID = m_SelectedBattle.AttackingFactionID
                ListForFactionID(m_SelectedBattle.AttackingFactionID)
            End If

        End Set
    End Property

    Public Sub RefreshList()
        ListForFactionID(owningFactionID)
    End Sub

    Private Sub ListForFactionID(ByVal factionid As Integer)
        'Dim chitKey As String
        Dim imageIndex As Integer

        Try
            tvwUnits.Nodes.Clear()

            If ptrGameData Is Nothing Then Exit Sub
            If m_SelectedBattle Is Nothing Then Exit Sub

            lblManagement.Text = Me.ptrGameData.Faction_g.Item("FACT" & factionid).FactionName & " units"

            For Each mem As ChitMember In m_SelectedBattle.InvolvedChits
                If mem.OwningFaction.FactionID = factionid Then
                    Dim nodie As TreeNode = NodesForChitmember(mem)
                    'tvwUnits.Nodes.Add(NodesForChitmember(mem))
                    tvwUnits.Nodes.Add(nodie)
                    nodie.EnsureVisible()
                End If
            Next


            '// now list rules assigned?
            'Dim asgnRule As AssignedRule

            If factionid = m_SelectedBattle.AttackingFactionID Then
                For Each ru As Rule In m_SelectedBattle.AttackerAssignedRules
                    Dim ruleNode As New TreeNode(ru.ptrRule.RuleName)
                    ruleNode.SelectedImageIndex = 6
                    ruleNode.ImageIndex = 6
                    tvwUnits.Nodes.Add(ruleNode)
                Next
            Else
                For Each ru As Rule In m_SelectedBattle.DefenderAssignedRules

                    Dim ruleNode As New TreeNode(ru.ptrRule.RuleName)
                    ruleNode.SelectedImageIndex = 6
                    ruleNode.ImageIndex = 6
                    tvwUnits.Nodes.Add(ruleNode)
                Next
            End If

        Catch ex As Exception
            HandleException("ctlBattleAssets", ex)
        End Try

    End Sub

    Private Function NodesForChitmember(ByVal mem As ChitMember) As TreeNode
        If mem.UnitType = GroundChitClass_cgd.Objective Then
            Return NodesForChitmember(DirectCast(mem, InstallationChit))
        ElseIf mem.UnitType = GroundChitClass_cgd.Unit Then
            Return NodesForChitmember(DirectCast(mem, ChitUnitTypeMember))
        ElseIf mem.UnitType = GroundChitClass_cgd.Battlegroup Then
            Return NodesForChitmember(DirectCast(mem, BattlegroupChit))
        End If
    End Function

    Private Function NodesForChitmember(ByVal inst As InstallationChit) As TreeNode
        Try
            Dim node As New ChitTreeNode(inst) '  TreeNode(inst.Name)
            node.ImageIndex = 0
            node.SelectedImageIndex = 0

            '// guess we can list rules
            Dim ret As RuleType
            For Each ret In inst.ReadyRules
                Dim snode As New TreeNode(ret.RuleName)
                snode.SelectedImageIndex = 6
                snode.ImageIndex = 6
                node.Nodes.Add(snode)
            Next

            If inst.Status = GroundUnitStatus_cgd.Dead Then
                node.ImageIndex = 1
                node.SelectedImageIndex = 1
            End If

            Return node
        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try
    End Function

    Private Function NodesForChitmember(ByVal unit As ChitUnitTypeMember) As TreeNode
        Try
            Dim node As New ChitTreeNode(unit)
            node.ImageIndex = 2
            node.SelectedImageIndex = 2
            If unit.Status = GroundUnitStatus_cgd.Reinforcing Then
                node.ImageIndex = 3
                node.SelectedImageIndex = 3
            End If

            If unit.Status = GroundUnitStatus_cgd.Dead Then
                node.ImageIndex = 1
                node.SelectedImageIndex = 1
            End If

            Return node
        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try
    End Function

    Private Function NodesForChitmember(ByVal bg As BattlegroupChit) As TreeNode
        Try
            Dim node As New ChitTreeNode(bg)

            For Each mem As ChitMember In bg.members
                node.Nodes.Add(NodesForChitmember(mem))
            Next

            If bg.Status = GroundUnitStatus_cgd.Dead Then
                node.SelectedImageIndex = 1
                node.ImageIndex = 1
            End If

            Return node

        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try
    End Function

#Region "Management Clicks"
    Private Sub btnEditElements_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditElements.Click
        Try
            'If lvwAssets.SelectedItems(0) Is Nothing Then Exit Sub
            'If lvwAssets.SelectedItems.Count = 0 Then Exit Sub
            'Dim selchit As ChitListviewItem = chitNode.GroundChit

            If Not TypeOf tvwUnits.SelectedNode Is ChitTreeNode Then Exit Sub

            Dim chitNode As ChitTreeNode = tvwUnits.SelectedNode
            Dim selectedChit As ChitMember = chitNode.Member

            ManageElements(selectedChit, ptrGameData)
        Catch ex As Exception
            HandleException("ctlEndOfCombatPhase.btnEditElements", ex)
        End Try

    End Sub

    Private Sub btnKillChit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnKillChit.Click
        Try
            'If lvwAssets.SelectedItems.Count = 0 Then Exit Sub
            'Dim selchit As ChitListviewItem = lvwAssets.SelectedItems(0)
            'Dim selectedChit As GroundChitType = m_SelectedBattle.InvolvedChits.Item(selchit.ChitKey)

            If Not TypeOf tvwUnits.SelectedNode Is ChitTreeNode Then Exit Sub

            Dim chitNode As ChitTreeNode = tvwUnits.SelectedNode
            Dim selectedChit As ChitMember = chitNode.Member

            If selectedChit.Status = GroundUnitStatus_cgd.Dead Then
                selectedChit.Status = GroundUnitStatus_cgd.Engaged
                '// "unkill" it.
                If selectedChit.UnitType = GroundChitClass_cgd.Objective Then
                    chitNode.ImageIndex = 0
                    chitNode.SelectedImageIndex = 0
                Else
                    chitNode.ImageIndex = 2
                    chitNode.SelectedImageIndex = 2
                End If
            Else
                KillChit(selectedChit)
                If selectedChit.Status = GroundUnitStatus_cgd.Dead Then
                    chitNode.ImageIndex = 1
                    chitNode.SelectedImageIndex = 1
                Else
                    '// return it to its normal image
                    Select Case selectedChit.UnitType
                        Case GroundChitClass_cgd.Battlegroup, GroundChitClass_cgd.Unit
                            chitNode.ImageIndex = 2
                            chitNode.SelectedImageIndex = 2
                        Case GroundChitClass_cgd.Objective
                            chitNode.ImageIndex = 0
                            chitNode.SelectedImageIndex = 0
                    End Select
                End If
            End If

            'ListForFactionID(owningFactionID)
            RaiseEvent UnitKilledOrUnkilled()

        Catch ex As Exception
            HandleException("ctlEndOfCombatPhase.btnEditElements", ex)
        End Try

    End Sub

    Private Sub btnOwner_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOwner.Click
        Try
            'If lvwAssets.SelectedItems(0) Is Nothing Then Exit Sub
            'If lvwAssets.SelectedItems.Count = 0 Then Exit Sub
            'Dim selchit As ChitListviewItem = lvwAssets.SelectedItems(0)
            'Dim selectedChit As GroundChitType = m_SelectedBattle.InvolvedChits.Item(selchit.ChitKey)

            If Not TypeOf tvwUnits.SelectedNode Is ChitTreeNode Then Exit Sub

            Dim chitNode As ChitTreeNode = tvwUnits.SelectedNode
            Dim selectedChit As ChitMember = chitNode.Member

            If selectedChit.UnitType <> GroundChitClass_cgd.Objective Then
                MessageBox.Show("You may only transfer ownership of Installations.", "Invalid Action", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Exit Sub
            End If

            ReassignOwner(selectedChit, ptrGameData)

            RaiseEvent ChitChangedOwnership()

            ListForFactionID(owningFactionID)

        Catch ex As Exception
            HandleException("ctlEndOfCombatPhase.btnOwner", ex)
        End Try
    End Sub

    Private Sub btnPointValue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPointValue.Click
        Try
            'If lvwAssets.SelectedItems(0) Is Nothing Then Exit Sub
            'If lvwAssets.SelectedItems.Count = 0 Then Exit Sub
            'Dim selchit As ChitListviewItem = lvwAssets.SelectedItems(0)
            'Dim selectedChit As GroundChitType = m_SelectedBattle.InvolvedChits.Item(selchit.ChitKey)

            If Not TypeOf tvwUnits.SelectedNode Is ChitTreeNode Then Exit Sub

            Dim chitNode As ChitTreeNode = tvwUnits.SelectedNode
            Dim selectedChit As ChitMember = chitNode.Member

            ChangePointValue(selectedChit)
        Catch ex As Exception
            HandleException("ctlEndOfCombatPhase.btnMovement", ex)
        End Try
    End Sub

    Private Sub btnMovement_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMovement.Click
        Try
            'If lvwAssets.SelectedItems(0) Is Nothing Then Exit Sub
            'If lvwAssets.SelectedItems.Count = 0 Then Exit Sub
            'Dim selchit As ChitListviewItem = lvwAssets.SelectedItems(0)
            'Dim selectedChit As GroundChitType = m_SelectedBattle.InvolvedChits.Item(selchit.ChitKey)

            If Not TypeOf tvwUnits.SelectedNode Is ChitTreeNode Then Exit Sub

            Dim chitNode As ChitTreeNode = tvwUnits.SelectedNode
            Dim selectedChit As ChitMember = chitNode.Member

            If selectedChit.UnitType = GroundChitClass_cgd.Objective Then Exit Sub

            ChangeMovementValue(selectedChit)
        Catch ex As Exception
            HandleException("ctlEndOfCombatPhase.btnMovement", ex)
        End Try
    End Sub
#End Region

    Private Class ChitListviewItem
        Inherits ListViewItem

        Private m_ChitKey As String

        Public Property ChitKey() As String
            Get
                Return m_ChitKey
            End Get
            Set(ByVal Value As String)
                m_ChitKey = Value
            End Set
        End Property
    End Class

End Class
